home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Symantec Versions / C03 Sound Recording / P02 Sound Handle / SoundHandle.c next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  3.5 KB  |  141 lines  |  [TEXT/KAHL]

  1. //____________________________________________________________
  2. //    SoundHandle.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8. //
  9. //    The "Graphics and Sound Programming for the Mac" book shows the variable theSound 
  10. //    having a data type of SndListHandle. The book does this in preparation for the call
  11. //    to SndRecord(). The book shows that the third parmater of this function as a 
  12. //    SndListHandle - as expected by the version of the Sound.h universal header file used
  13. //    by Metrowerks at the time of this writing. Symantec uses a different version of 
  14. //    Sound.h. If Symantec updates the Apple Universal Header files, you may get a compilation 
  15. //    error. If you do, it will concern code in the PlaySoundResourceSynch() function.
  16. //    Here are the changes you'll need to make. Change the code to match the code in the book.
  17.  
  18.  
  19.  
  20. //____________________________________________________________
  21.  
  22. #include <Sound.h>
  23. #include <SoundInput.h>
  24.  
  25.  
  26. //____________________________________________________________
  27.  
  28. void     InitializeToolbox( void );
  29. Boolean  IsSoundInputAvailable( void );
  30. OSErr    RecordSoundToMemory( Handle * );
  31. OSErr    PlaySoundSynchFromHandle( Handle );
  32.  
  33.  
  34. //____________________________________________________________
  35.  
  36. #define    kHeapReserve        75 * 1024 
  37.  
  38.  
  39. //____________________________________________________________
  40.  
  41. void  main( void )
  42. {
  43.    NumVersion  theSndMgrVers;
  44.    OSErr       theError;
  45.    Handle      theSound;
  46.    Boolean     soundInputPresent;   
  47.    
  48.    InitializeToolbox();
  49.  
  50.    MaxApplZone();
  51.  
  52.    theSndMgrVers = SndSoundManagerVersion();   
  53.    if ( theSndMgrVers.majorRev < 3 )
  54.       ExitToShell();
  55.  
  56.    soundInputPresent = IsSoundInputAvailable();
  57.    if ( soundInputPresent == false )
  58.       ExitToShell();
  59.  
  60.    theError = RecordSoundToMemory( &theSound );
  61.    if ( theError == userCanceledErr )
  62.       ExitToShell();
  63.       
  64.    theError = PlaySoundSynchFromHandle( theSound );   
  65.    if ( theError != noErr )
  66.       ExitToShell();
  67.       
  68.    ReleaseResource( (Handle)theSound );
  69. }
  70.  
  71.  
  72. //____________________________________________________________
  73.  
  74. OSErr  RecordSoundToMemory( Handle *theSound )
  75. {
  76.    OSErr  theError;
  77.    Point  theCorner = { 50, 20 }; 
  78.    long   theTotalHeap;
  79.    long   theContigMem;
  80.    
  81.    PurgeSpace( &theTotalHeap, &theContigMem );
  82.  
  83.    *theSound = NewHandle( theContigMem - kHeapReserve );
  84.    
  85.    theError = SndRecord( nil, theCorner, siBestQuality, theSound );
  86.    
  87.    return ( theError );
  88. }
  89.  
  90.  
  91. //____________________________________________________________
  92.  
  93. OSErr  PlaySoundSynchFromHandle( Handle theHandle )
  94. {
  95.    OSErr   theError;
  96.    
  97.    if ( theHandle != nil )
  98.    {
  99.       HLock( theHandle );
  100.          theError = SndPlay( nil, theHandle, false );
  101.       HUnlock( theHandle );
  102.  
  103.       return ( theError );
  104.    }
  105. }
  106.  
  107.  
  108. //____________________________________________________________
  109.  
  110. Boolean  IsSoundInputAvailable( void )
  111. {
  112.    OSErr    theError;
  113.    long     theResult;
  114.    Boolean  inputAvail;
  115.    
  116.    theError = Gestalt( gestaltSoundAttr, &theResult );
  117.    if ( theError != noErr )
  118.       ExitToShell();
  119.       
  120.    inputAvail = theResult & ( 1 << gestaltHasSoundInputDevice );  
  121.    if ( inputAvail > 0 )
  122.       return ( true );
  123.    else
  124.       return ( false );
  125. }
  126.  
  127.  
  128. //____________________________________________________________
  129.  
  130. void  InitializeToolbox( void )
  131. {
  132.    InitGraf( &qd.thePort );
  133.    InitFonts();
  134.    InitWindows();
  135.    InitMenus();
  136.    TEInit();
  137.    InitDialogs( 0L );
  138.    FlushEvents( everyEvent, 0 );
  139.    InitCursor();
  140. }
  141.